(page requirements)

bin2hex - script to convert data files into source code arrays

Looking for a way to convert a binary (or even a text) file into a Perl, Pascal/Delphi, C/C++, or Java source code array? For example, you may want to do this to include a GIF or other binary file directly inside your Perl CGI script, rather than reading the data from a file to reduce file access. If you're writing Windows programs, you could use the same method to store resource files in your executable files.
 
The bin2hex.pl script will take a binary file name and output a source code array, in the specified language, necessary to recreate the input data inside a program.
 
To run the script, first make sure that you have a Perl interpreter installed on your system and download the bin2hex.pl script. Then execute the following command from your command prompt (assuming you want to convert a file named banner.gif to a Perl string):
 
perl bin2hex.pl banner.gif 0 >source.txt
Listing #1 : TEXT code. Download demo1.bat (0.17 KB).
 
You will end-up with a source.txt file with content similar to the following:
 
# begin binary data:
$bin_data = # 42
"x47x49x46x38x39x61x01".
"x00x01x00x80x00x00x00x00".
"x00xFFxFFxFFx21xF9x04".
"x01x00x00x00x00x2Cx00x00".
"x00x00x01x00x01x00x40".
"x02x01x44x00x3B";
# end binary data. size = 42 bytes
Listing #2 : TEXT code. Download result1.txt (0.27 KB).
 

 
bin2hex.pl script
 
Usage:
perl bin2hex.pl <binary_file_name> <language_id>
 
binary_file_name
    The input file name
 
language_id
    The language identifier
    (source code array will be generated in this language):
        0 - Perl or similar language(default)
        1 - C / C++ / Java or similar language
        2 - Pascal / Delphi or similar language
 
 
#
# bin2hex.pl by Chami.com
# http://www.chami.com/tips/
#

# number of characters per line
$chars_per_line = 15;

# -------------------------------------

# language id
#
# 0 = Perl (default)
# 1 = C / C++
# 2 = Pascal / Delphi
#
$lang  = $ARGV[1];

$rem_begin  = "begin binary data:";
$rem_end    = "end binary data.";

# initialize for Perl strings
# by default
$_var       = "# $rem_beginn".
              "$bin_data = # %dn";
$_begin     = """;
$_end       = "";n";
$_break     = "".n"";
$_format    = "\x%02X";
$_separator = "";
$_comment   = "# $rem_end ".
              "size = %d bytes";


# C / C++
if(1 == $lang)
{
  $_var       = "/* $rem_begin */n".
                "char bin_data[] = ".
                "/* %d */n";
  $_begin     = "{";
  $_end       = "};n";
  $_break     = "n";
  $_format    = "0x%02X";
  $_separator = ",";
  $_comment   = "/* $rem_end ".
                "size = %d bytes */";
}
elsif(2 == $lang)
{
  $_var       = "{ $rem_begin }n".
                "const bin_data : ".
                "array [1..%d] of ".
                "byte = n";
  $_begin     = "(";
  $_end       = ");n";
  $_break     = "n";
  $_format    = "$%02X";
  $_separator = ",";
  $_comment   = "{ $rem_end ".
                "size = %d bytes }";
}

if(open(F, "<".$ARGV[0]))
{
  binmode(F);

  $s = '';
  $i = 0;
  $count = 0;
  $first = 1;
  $s .= $_begin;
  while(!eof(F))
  {
    if($i >= $chars_per_line)
    {
      $s .= $_break;
      $i = 0;
    }
    if(!$first)
    {
      $s .= $_separator;
    }
    $s .= sprintf(
            $_format, ord(getc(F)));
    ++$i;
    ++$count;
    $first = 0;
  }
  $s .= $_end;
  $s .= sprintf $_comment, $count;
  $s .= "nn";

  $s = "n".sprintf($_var, $count).$s;

  print $s;

  close( F );
}
else
{
  print
    "bin2hex.pl by Chami.comn".
    "n".
    "usage:n".
    "  perl bin2hex.pl <binary file>".
    " <language id>n".
    "n".
    "  <binary file> : path to the ".
    "binary filen".
    "  <language id> : 0 = Perl, ".
    "1 = C/C++/Java, ".
    "2 = Pascal/Delphin".
    "n";
}
Listing #3 : PERL code. Download bin2hex.pl (0.91 KB).
 
 
Applicable Keywords : C Language, Common Gateway Interface, C++, Delphi, HTML, JavaScript, Perl, Programming, PerlScript, Source Code, Software
 
 
 
Copyright © 2009 Chami.com. All Rights Reserved. | Advertise | Created in HTML Kit editor